package com.htudata.trigger.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Objects;

import com.htudata.alarm.pojo.po.AlarmLevelPO;
import com.htudata.alarm.service.AlarmLevelService;
import com.htudata.trigger.dao.TriggerDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TriggerService {

  @Autowired
  private TriggerDao triggerDao;
  
  @Autowired
  private AlarmLevelService alarmLevelService;
  
  private final int ZABBIXTYPE = 0;
  
  private final int LOCALTYPE = 1;
  
  public JSONArray getTriggerList(String flowId, String nodeId, int detail, String triggerName) {
    JSONArray triggersData = new JSONArray();
    if (StringUtils.isEmpty(flowId) || StringUtils.isEmpty(nodeId))
      return triggersData; 
    if (flowId.equals("0") && nodeId.equals("0")) {
      JSONArray localTriggers = this.triggerDao.selectAllLocalTriggers(triggerName);
      triggersData = parseTriggerListData(null, localTriggers, detail);
    } else if ((flowId.equals("0") && !nodeId.equals("0")) || (!flowId.equals("0") && !nodeId.equals("0"))) {
      JSONArray localTriggers = this.triggerDao.selectLocalTriggersByNodeIdOrFlowId(flowId, nodeId);
      triggersData = parseTriggerListData(null, localTriggers, detail);
    } else if (!flowId.equals("0") && nodeId.equals("0")) {
      JSONArray localTriggers = this.triggerDao.selectLocalTriggersByNodeIdOrFlowId(flowId, nodeId);
      triggersData = parseTriggerListData(null, localTriggers, detail);
    } 
    return triggersData;
  }
  
  public JSONArray parseTriggerListData(JSONArray zabbixTriggers, JSONArray localTriggers, int detail) {
    JSONArray triggerData = new JSONArray();
    List<AlarmLevelPO> alarmLevelList = this.alarmLevelService.getAlarmLevelList(null);
    if (isValidJSONArray(zabbixTriggers))
      zabbixTriggers.forEach(zabbixTrigger -> {
            JSONObject zabbixTriggerObject = (JSONObject)zabbixTrigger;
            JSONObject zabbixTriggerJson = parseTriggerData(zabbixTriggerObject, 0, detail, alarmLevelList);
            triggerData.add(zabbixTriggerJson);
          }); 
    if (isValidJSONArray(localTriggers))
      localTriggers.forEach(localTrigger -> {
            JSONObject localTriggerObject = (JSONObject)localTrigger;
            JSONObject localTriggerJson = parseTriggerData(localTriggerObject, 1, detail, alarmLevelList);
            triggerData.add(localTriggerJson);
          }); 
    return triggerData;
  }
  
  public JSONObject parseTriggerData(JSONObject triggerObject, int type, int detail, List<AlarmLevelPO> alarmLevelList) {
    JSONObject triggerJson = new JSONObject(true);
    triggerJson.put("nodeId", triggerObject.getString("nodeId"));
    triggerJson.put("triggerId", Long.valueOf(triggerObject.getLongValue("triggerId")));
    triggerJson.put("triggerName", triggerObject.getString("name"));
    triggerJson.put("type", Integer.valueOf(type));
    triggerJson.put("flowName", triggerObject.getString("flowName"));
    triggerJson.put("nickName", triggerObject.getString("nickName"));
    triggerJson.put("comptName", triggerObject.getString("comptName"));
    if (detail == 1) {
      AlarmLevelPO alarmLevelPO = AlarmLevelService.getAlarmLevelInfo(alarmLevelList, triggerObject.getIntValue("level"));
      triggerJson.put("level", Integer.valueOf(alarmLevelPO.getLevel()));
      triggerJson.put("color", alarmLevelPO.getColor());
      triggerJson.put("levelName", alarmLevelPO.getName());
    } 
    return triggerJson;
  }
  
  public boolean isValidJSONObject(JSONObject jsonObject) {
    if (!Objects.isNull(jsonObject) && jsonObject.size() > 0)
      return true; 
    return false;
  }
  
  public boolean isValidJSONArray(JSONArray jsonArray) {
    if (!Objects.isNull(jsonArray) && jsonArray.size() > 0)
      return true; 
    return false;
  }
}
